package com.example.smartall.ui;

import androidx.annotation.NonNull;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smartall.R;
import com.example.smartall.adapter.PervasiveAdapter;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.entity.HospitalBannerBean;
import com.example.smartall.entity.HospitalBean;
import com.example.smartall.ui.base.BaseActivity;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * 医院门诊主页
 */
public class HospitalActivity extends BaseActivity {
    private int pageNum = 1;

    private ImageView imBack;
    private EditText etSearch;
    private Banner banner;
    private ListView lvHospital;

    private MHandler handler;

    private List<HospitalBannerBean.DataBean> bannerList = new ArrayList<>();
    private List<HospitalBean.RowsBean> hospitalList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital);

        handler = new MHandler();

        init();
        getBannerData();
        getHospitalData();
        initEvent();
    }

    @Override
    public void init() {
        imBack = (ImageView) findViewById(R.id.im_back);
        etSearch = (EditText) findViewById(R.id.et_search);
        banner = (Banner) findViewById(R.id.banner);
        lvHospital = (ListView) findViewById(R.id.lv_hospital);
    }

    @Override
    public void initEvent() {
        imBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HospitalActivity.this.finish();
            }
        });
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    //TODO 搜索跳转
                }
                return false;
            }
        });
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int i) {
                //TODO 轮播图跳转
            }
        });
        lvHospital.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getThis(), HospitalDetailsActivity.class);
                intent.putExtra("hospitalId",hospitalList.get(i).getId());
                startActivity(intent);
            }
        });
    }

    private void getBannerData() {
        if (bannerList.size() > 0) return;
        Api.config(ApiConfig.HOSPITAL_BANNER, null).getRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                HospitalBannerBean bean = new Gson().fromJson(res, HospitalBannerBean.class);
                if (bean.getCode() == 200) {
                    bannerList = bean.getData();
                    handler.sendEmptyMessage(0x01);
                }
            }

            @Override
            public void onFailure(Exception e) {
                showToastSync("加载失败");
            }
        });
    }

    private void getHospitalData(){
        if (hospitalList.size() > 0) return;
        Api.config(ApiConfig.HOSPITAL_LIST,null).getRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                HospitalBean bean = new Gson().fromJson(res,HospitalBean.class);
                if (bean.getCode() == 200){
                    hospitalList = bean.getRows();
                    handler.sendEmptyMessage(0x02);
                }
            }

            @Override
            public void onFailure(Exception e) {
                showToastSync("加载失败");
            }
        });
    }

    class MHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x01:
                    List<String> bannerUrls = new ArrayList<>();
                    for (HospitalBannerBean.DataBean bean:bannerList) {
                        bannerUrls.add(ApiConfig.BASE_URL + bean.getImgUrl());
                    }
                    banner.setImages(bannerUrls);
                    banner.setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object o, ImageView imageView) {
                            Glide.with(HospitalActivity.this).load(o).into(imageView);
                        }
                    });
                    banner.setDelayTime(2000);
                    banner.isAutoPlay(true);
                    banner.start();
                    banner.startAutoPlay();
                    break;
                case 0x02:
                    lvHospital.setAdapter(new HospitalListAdapter(hospitalList));
                    break;
            }
        }
    }

     /**
      * @Description:    第一次使用万能适配器 注释模板好像没配好，一些数据不显示，日期还是自己敲的
      * @Author:         abbas
      * @CreateDate:     2022/1/23
      */
    class HospitalListAdapter extends PervasiveAdapter<HospitalBean.RowsBean>{

        public HospitalListAdapter(List<HospitalBean.RowsBean> datas) {
            super(datas);
        }

        @Override
        protected void setUI(ViewHolder holder, int position, Context context) {
            ImageView img = (ImageView) holder.getItemView(R.id.img);
            TextView title = (TextView) holder.getItemView(R.id.title);
            TextView subtitle = (TextView) holder.getItemView(R.id.subtitle);
            TextView content = (TextView) holder.getItemView(R.id.content);

            Glide.with(context).load(ApiConfig.BASE_URL + hospitalList.get(position).getImgUrl()).into(img);
            title.setText(hospitalList.get(position).getHospitalName());
            subtitle.setText("评分等级："+hospitalList.get(position).getLevel());
            content.setText(hospitalList.get(position).getBrief().substring(3));
        }

        @Override
        protected int getLayoutId() {
            return R.layout.common_list_item;
        }
    }
}